﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Wizard.UI;
using System.Diagnostics;

namespace DVRMS_Renamer
{
    public partial class wizPage4 : Wizard.UI.InternalWizardPage
    {
        private frmMain mobjOwnerForm;

        public wizPage4(frmMain OwnerForm)
        {
            InitializeComponent();

            mobjOwnerForm = OwnerForm;

            linkURL.Visible = false;
            linkPlay.Visible = false;
        }

        private void wizPage4_WizardPageSetActive(object sender, CancelEventArgs e)
        {
            //TODO:DS SetWizardButtons(WizardButtons.Back | WizardButtons.Next);
        }

        private void wizPage4_WizardPageActivated(object sender, EventArgs e)
        {
            this.Refresh();
            FormLayout(Globals.FormLayouts.flProgress);
            Common.ProcessItems(this, pgrProgress, lblWorking, lstItems);
            FormLayout(Globals.FormLayouts.flStandard);

            SetWizardButtons(WizardButtons.Next);
        }

        private void wizPage4_Load(object sender, EventArgs e)
        {
            lstItems.FullRowSelect = true;
            lstItems.View = View.Details;
            lstItems.HeaderStyle = ColumnHeaderStyle.Nonclickable;
            lblWorking.Text = "Working...";

            ColumnHeader col1 = new ColumnHeader();
            ColumnHeader col2 = new ColumnHeader();
            ColumnHeader col3 = new ColumnHeader();
            ColumnHeader col4 = new ColumnHeader();
            ColumnHeader col5 = new ColumnHeader();

            col1.Text = "Origninal Filename";
            col2.Text = "New Filename";
            col3.Text = "Confidence";
            col4.Text = "Sub Title";
            col5.Text = "Sub Title Description";

            lstItems.Columns.AddRange(new ColumnHeader[] { col1, col2, col3, col4, col5 });
        }

        private void wizPage4_WizardNext(object sender, WizardPageEventArgs e)
        {
            Globals.selectedFilesToRename.Clear();

            for (int i = 0; i < lstItems.CheckedItems.Count; i++)
            {
                File theFile = (File)lstItems.CheckedItems[i].Tag;
                Globals.selectedFilesToRename.Add(theFile);
            }
        }

        private void FormLayout(Globals.FormLayouts fl)
        {
            lblWorking.Visible = (fl == Globals.FormLayouts.flProgress);
            pgrProgress.Visible = (fl == Globals.FormLayouts.flProgress);
            lstItems.Visible = (fl == Globals.FormLayouts.flStandard);

            if (fl == Globals.FormLayouts.flProgress)
            {
                Banner.Title = "Matching Episode";
                Banner.Subtitle = "Matching the files to episodes on thetvdb.com";
            }
            else
            {
                Banner.Title = "Select Files";
                Banner.Subtitle = "Select which files you would like to rename";
            }
            this.Refresh();
        }

        private void linkURL_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                string url = (string)((LinkLabel)sender).Tag;
                Process p = new Process();
                p.StartInfo.FileName = url;
                p.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void linkPlay_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            try
            {
                string video = (string)((LinkLabel)sender).Tag;
                Process p = new Process();
                p.StartInfo.FileName = video;
                p.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void lstItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            File objFile = null;
            
            if (((ListView)sender).SelectedItems.Count == 1)
            {
                objFile = ((File)((ListView)sender).SelectedItems[0].Tag);
            }

            if (objFile != null)
            {
                linkURL.Visible = true;
                linkURL.Text = objFile.urlCaption;
                linkURL.Tag = objFile.url;

                linkPlay.Visible = true;
                linkPlay.Tag = objFile.sourcePath + objFile.originalFileName;
            }
            else
            {
                linkURL.Visible = false;
                linkPlay.Visible = false;
            }
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File objFile = null;

            if (lstItems.SelectedItems.Count == 1)
            {
                objFile = (File)lstItems.SelectedItems[0].Tag;
            }

            if (objFile != null)
            {
                Clipboard.SetText(objFile.subTitleDescription);
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            File objFile = null;

            if (lstItems.SelectedItems.Count == 1)
            {
                objFile = (File)lstItems.SelectedItems[0].Tag;
            }

            if (objFile != null)
            {
                if (MessageBox.Show("Are you sure you want to delete the selected file?", "Delete File", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
                {
                    // TODO:DS Perform this in a try...catch
                    System.IO.File.Delete(objFile.sourcePath + objFile.originalFileName);
                    lstItems.Items.Remove(lstItems.SelectedItems[0]);
                    
                    // TODO:DS Remove from the collection
                    //mobjOwnerForm.selectedFilesToScan.rem
                }
            }
        }

        private void playVideoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            linkPlay_LinkClicked(this, null);
        }

        private void viewOnTheWebToolStripMenuItem_Click(object sender, EventArgs e)
        {
            linkURL_LinkClicked(this, null);
        }
    }
}
